﻿<Window x:Class="MLifterSettingsManager.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MLifterSettingsManager="clr-namespace:MLifterSettingsManager"
    xmlns:winform="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 
    Title="MemoryLifter Settings Manager" Height="700" Width="1000" x:Name="windowMain" WindowStartupLocation="CenterScreen" Icon="Icons/App.png" 
        MinWidth="780" MinHeight="540" IsEnabled="{Binding ElementName=filterSortControl, Path=MainWindowEnabled, Mode=TwoWay}">
    <Window.Resources>
        <MLifterSettingsManager:ByteToStringConverter x:Key="byteToStringConverter" />
        <MLifterSettingsManager:NullToEnabledConverter x:Key="nullToEnabledConverter" />
        <MLifterSettingsManager:TreeViewItemToVisibilityConverter x:Key="treeViewItemToVisibilityConverter" />
        <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />
        <MLifterSettingsManager:BoolToBrushConverter x:Key="boolToBrushConverter" />
        <MLifterSettingsManager:SettingsToImageConverter x:Key="settingsToImageConverter" />

        <Style TargetType="{x:Type TextBlock}" x:Key="textBlockTreeViewItemStyle">
            <Setter Property="TextBlock.TextDecorations">
                <Setter.Value>
                    <TextDecorationCollection>
                        <TextDecoration PenThicknessUnit="Pixel">
                            <TextDecoration.Pen>
                                <Pen Thickness="2" Brush="{Binding Path=HasCustomSettings, Converter={StaticResource boolToBrushConverter}}" />
                            </TextDecoration.Pen>
                        </TextDecoration>
                    </TextDecorationCollection>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="24" />
            <RowDefinition />
            <RowDefinition Height="25" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400" MaxWidth="1000" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Menu Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" x:Name="menuMain">
            <MenuItem Header="_Learning Module">
                <MenuItem Header="_Open..." x:Name="menuItemOpen" Click="menuItemOpen_Click" />
                <MenuItem Name="menuItemPublish" Header="_Publish..." Click="menuItemPublish_Click" />
                <MenuItem Name="menuItemOptimizeAudio" Header="_Optimize Audio" Click="menuItemOptimizeAudio_Click"/>
                <MenuItem Name="menuItemExportToXml" Header="Export to _XML" Click="menuItemExportToXml_Click"/>
                <Separator />
                <MenuItem Header="_Quit" x:Name="menuItemExit" Click="menuItemExit_Click" />
            </MenuItem>
            <MenuItem Header="_Tools">
                <MenuItem Name="menuItemAutoConvertPublish" Header="_Batch Job: Convert + Publish" Click="menuItemAutoConvertPublish_Click"/>
                <MenuItem Name="menuItemCreateExtension" Header="_Extension Manager..." Click="menuItemCreateExtension_Click"/>
            </MenuItem>
        </Menu>
        <TabControl Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Name="tabControlSettingsManager">
            <TabItem Header="Learning Module Settings" Name="tabItemLMSettings">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="25" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="400" MaxWidth="1000" MinWidth="100" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <MLifterSettingsManager:FilterAndSortControl Grid.Column="0" Grid.Row="0" x:Name="filterSortControl" />

                    <TreeView Grid.Column="0" Grid.Row="1" x:Name="treeViewLearningModule" VirtualizingStackPanel.IsVirtualizing="True" Margin="0,0,3,0"
                              SelectedItemChanged="treeViewLearningModule_SelectedItemChanged">
                        <TreeView.ItemContainerStyle>
                            <Style TargetType="{x:Type TreeViewItem}">
                                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                            </Style>
                        </TreeView.ItemContainerStyle>
                        <TreeView.Resources>

                            <!-- LearningModule -->
                            <HierarchicalDataTemplate DataType="{x:Type MLifterSettingsManager:LearningModuleTreeViewItem}" ItemsSource="{Binding Path=Chapters}">
                                <StackPanel Orientation="Horizontal">
                                    <Image Source="Resources\learning_16.png" Width="16" Height="16" Margin="0,0,5,0" />
                                    <TextBlock Text="{Binding Title}" Style="{StaticResource textBlockTreeViewItemStyle}" />
                                    <StackPanel.ContextMenu>
                                        <ContextMenu x:Name="contextMenuLearningModule" Opened="contextMenuLearningModule_Opened" Tag="{Binding Path=.}">
                                            <MenuItem x:Name="menuItemLMDeselectAllCards" Header="Deselect all Cards" Click="menuItemLMDeselectAllCards_Click" Tag="{Binding Path=.}" />
                                        </ContextMenu>
                                    </StackPanel.ContextMenu>
                                </StackPanel>
                            </HierarchicalDataTemplate>

                            <!-- Chapter -->
                            <HierarchicalDataTemplate DataType="{x:Type MLifterSettingsManager:ChapterTreeViewItem}" ItemsSource="{Binding Cards}">
                                <StackPanel Orientation="Horizontal">
                                    <Image Source="Resources\chapter_16.png" Width="16" Height="16" Margin="0,0,5,0" />
                                    <TextBlock Text="{Binding Title}"  Style="{StaticResource textBlockTreeViewItemStyle}"/>
                                    <TextBlock Text=" " Style="{StaticResource textBlockTreeViewItemStyle}"/>
                                    <TextBlock Text="{Binding Path=Cards.Count, StringFormat=({0})}"  Style="{StaticResource textBlockTreeViewItemStyle}"/>
                                    <StackPanel.ContextMenu>
                                        <ContextMenu x:Name="contextMenuChapter" Opened="contextMenuChapter_Opened" Tag="{Binding Path=.}">
                                            <MenuItem x:Name="menuItemSelectAllCards" Header="Select all Cards" Click="menuItemSelectAllCards_Click" Tag="{Binding Path=.}" />
                                            <MenuItem x:Name="menuItemDeselectAllCards" Header="Deselect all Cards" Click="menuItemDeselectAllCards_Click" Tag="{Binding Path=.}" />
                                            <MenuItem x:Name="menuItemInvertSelection" Header="Invert selection" Click="menuItemInvertSelection_Click" Tag="{Binding Path=.}" />
                                        </ContextMenu>
                                    </StackPanel.ContextMenu>
                                </StackPanel>
                            </HierarchicalDataTemplate>

                            <!-- Card -->
                            <DataTemplate DataType="{x:Type MLifterSettingsManager:CardTreeViewItem}">
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox IsChecked="{Binding IsChecked}" Height="16" Width="16" Focusable="False" />
                                    <TextBlock Text="{Binding Question}"  Style="{StaticResource textBlockTreeViewItemStyle}"/>
                                    <TextBlock Text=" / " Style="{StaticResource textBlockTreeViewItemStyle}"/>
                                    <TextBlock Text="{Binding Answer}" Style="{StaticResource textBlockTreeViewItemStyle}"/>
                                </StackPanel>
                            </DataTemplate>

                        </TreeView.Resources>
                    </TreeView>

                    <GridSplitter Grid.Column="0" Grid.Row="1" Width="3" />

                    <ScrollViewer Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
                        <StackPanel>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <ContentControl Grid.Row="0" Content="{Binding ElementName=treeViewLearningModule, Path=SelectedItem}">
                                <ContentControl.Resources>

                                    <DataTemplate DataType="{x:Type MLifterSettingsManager:LearningModuleTreeViewItem}">
                                        <Border BorderBrush="Green" BorderThickness="1" Background="LightGreen" CornerRadius="4" Margin="5,0,0,0">
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Margin="5" FontSize="15" FontWeight="Bold">LearningModule:</TextBlock>
                                                <TextBlock Margin="5" FontSize="15" FontWeight="Bold" Text="{Binding Title}" />
                                            </StackPanel>
                                        </Border>
                                    </DataTemplate>

                                    <DataTemplate DataType="{x:Type MLifterSettingsManager:ChapterTreeViewItem}">
                                        <Border BorderBrush="Green" BorderThickness="1" Background="LightGreen" CornerRadius="4" Margin="5,0,0,0">
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Margin="5" FontSize="15" FontWeight="Bold">Chapter:</TextBlock>
                                                <TextBlock Margin="5" FontSize="15" FontWeight="Bold" Text="{Binding Title}" />
                                            </StackPanel>
                                        </Border>
                                    </DataTemplate>

                                    <DataTemplate DataType="{x:Type MLifterSettingsManager:CardTreeViewItem}">
                                        <StackPanel>
                                            <Border BorderBrush="Green" BorderThickness="1" Background="LightGreen" CornerRadius="4" Margin="5,0,0,0">
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Margin="5" FontSize="15" FontWeight="Bold">Card:</TextBlock>
                                                    <TextBlock Margin="5" FontSize="15" FontWeight="Bold">(Id: </TextBlock>
                                                    <TextBlock Margin="5" FontSize="15" FontWeight="Bold" Text="{Binding Id}" />
                                                    <TextBlock Margin="5" FontSize="15" FontWeight="Bold">)</TextBlock>
                                                </StackPanel>
                                            </Border>
                                            <MLifterSettingsManager:PreviewCardControl Card="{Binding Card}" LearningModule="{Binding LearningModule}" DataContext="{Binding}" />
                                        </StackPanel>
                                    </DataTemplate>

                                </ContentControl.Resources>
                            </ContentControl>

                                <StackPanel Grid.Row="1" Visibility="{Binding ElementName=treeViewLearningModule, Path=SelectedItem, Converter={StaticResource treeViewItemToVisibilityConverter}}">
                                    <MLifterSettingsManager:SettingsControl x:Name="settingsControlMain" />
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                        <Button x:Name="buttonApplySettings" Click="buttonApplySettings_Click" Width="120" Margin="5">Apply Settings</Button>
                                        <Button x:Name="buttonClearSettings" Click="buttonClearSettings_Click" Width="120" Margin="5">Clear Settings</Button>
                                    </StackPanel>
                                </StackPanel>

                            <StackPanel Orientation="Horizontal" Grid.Row="2">
                                <TextBlock FontWeight="Bold" FontSize="14" Text="Apply Settings for: " Margin="5,0,0,0" Visibility="{Binding ElementName=treeViewLearningModule, Path=SelectedItem, Converter={StaticResource treeViewItemToVisibilityConverter}}" />
                                <TextBlock FontWeight="Bold" FontSize="14" Text="{Binding ElementName=treeViewLearningModule, Path=SelectedItem.TreeViewItemType}" />
                            </StackPanel>
                        </Grid>
                    </StackPanel>
                    </ScrollViewer>
                    <ProgressBar Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" x:Name="progressBarLoad" IsIndeterminate="False"
                                 Value="{Binding ElementName=filterSortControl, Path=ProgressBarValue}" Maximum="{Binding ElementName=filterSortControl, Path=ProgressBarMaximum}">
                    </ProgressBar>
                    <TextBlock x:Name="textBlockProgressBar" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center"
                               Text="{Binding ElementName=filterSortControl, Path=ProgressBarText, Mode=TwoWay}" />
                </Grid>
            </TabItem>
            <TabItem Header="Learning Module Extensions" x:Name="TabItemExtensions">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <ListView Grid.Row="0" Grid.Column="0" x:Name="listViewExtensions">
                        <ListView.View>
                            <GridView>
                                <GridView.ColumnHeaderTemplate>
                                    <DataTemplate>
                                        <Label Content="{Binding}"></Label>
                                    </DataTemplate>
                                </GridView.ColumnHeaderTemplate>

                                <GridViewColumn Header="Guid" DisplayMemberBinding="{Binding Path=Id}" />
                                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" Width="120" />
                                <GridViewColumn Header="Type" DisplayMemberBinding="{Binding Path=Type}" Width="100" />
                                <GridViewColumn Header="Version" DisplayMemberBinding="{Binding Path=Version}" Width="100" />
                                <GridViewColumn Header="Start File" DisplayMemberBinding="{Binding Path=StartFile}" Width="120" />
                                <GridViewColumn Header="Data" Width="150" >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                                <TextBlock Margin="5,0,5,0" Text="{Binding Path=Data.Length, Converter={StaticResource byteToStringConverter}}" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>

                    <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Top">
                        <Button x:Name="buttonAddExtension" Margin="5" Height="25" Click="buttonAddExtension_Click">Add new Extension</Button>
                        <Button x:Name="buttonRemoveExtension" Margin="5" Height="25" Click="buttonRemoveExtension_Click"
                                IsEnabled="{Binding ElementName=listViewExtensions, Path=SelectedItem, Converter={StaticResource nullToEnabledConverter}}">
                            Remove Extension</Button>
                    </StackPanel>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>
